package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.client.wemedia.IWemediaClient;
import com.heima.model.common.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmUserDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.admin.ApUserRealname;
import com.heima.model.common.pojos.user.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.AuthorMapper;
import com.heima.user.service.AuthorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author [二次元改变世界]
 * 创建时间：🎵 2024/8/1  10:56  星期四
 * 项目名称: 💖 adminProject
 * 包名: 💗 com.heima.user.service.impl
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApUserRealname> implements AuthorService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private IWemediaClient iWemediaClient;
    @Override
    public ResponseResult listQuery(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getPage() == null) {
            dto.setPage(0);
        }
        if (dto.getSize() == null) {
            dto.setSize(10);
        }
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page, Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus()));
        ResponseResult responseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getMsg() == null) {
            dto.setMsg("未设置理由,直接驳回");
        }
        ApUserRealname realname = getById(dto.getId());
        if (realname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        realname.setReason(dto.getMsg());
        realname.setStatus((short) 2);
        updateById(realname);
        return ResponseResult.okResult("操作成功");
    }

    /**
     * 审核成功
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult authPass(AuthDto dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getMsg() == null) {
            dto.setMsg("审核通过");
        }
        ApUserRealname realname = getById(dto.getId());
        if (realname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        realname.setStatus((short) 9);
        realname.setReason(dto.getMsg());
        updateById(realname);
        //开通自媒体账号
        ApUserRealname apUserRealname = getById(dto.getId());
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        WmUserDto wmUserDto = new WmUserDto();
        BeanUtils.copyProperties(apUser, wmUserDto);
        wmUserDto.setUserId(apUser.getId());
        iWemediaClient.add(wmUserDto);
        return ResponseResult.okResult("操作成功");
    }
}
